package com.happyfamily.springboot.crowdsourcingplatform.controller.weng;

import com.alibaba.fastjson.JSONArray;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.RoleDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysPermission;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.PermissionService;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.ProcessService;
import com.happyfamily.springboot.crowdsourcingplatform.util.WormUtils;
import com.happyfamily.springboot.crowdsourcingplatform.util.WormUtilsTwo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api-back/permission")
@Slf4j
public class PermissionController
{
	@Autowired
	private PermissionService permissionService;


	@RequestMapping(value = "/listAllPermission", method = RequestMethod.GET)
	@ResponseBody
	//@PreAuthorize("hasAuthority('sys:menu:query')")
	public Results<JSONArray> listAllPermission()
	{
		return permissionService.listAllPermission();
	}

	@RequestMapping(value = "/listAllPermissionByRoleId", method = RequestMethod.GET)
	@ResponseBody
	public Results<SysPermission> listAllPermissionByRoleId(RoleDto roleDto)
	{
		log.info(getClass().getName() + " : param =  " + roleDto);
		return permissionService.listByRoleId(roleDto.getId().intValue());
	}

	@GetMapping("/menuAll")
	@ResponseBody
	//@PreAuthorize("hasAuthority('sys:menu:query')")
	public Results getMenuAll()
	{
		return permissionService.getMenuAll();
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addPermission(Model model)
	{
		model.addAttribute("sysPermission", new SysPermission());
		return "backPage/permission-add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	//@PreAuthorize("hasAuthority('sys:menu:add')")
	public Results<SysPermission> savePermission(@RequestBody SysPermission permission)
	{
		return permissionService.save(permission);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String editPermission(Model model, SysPermission permission)
	{
		model.addAttribute("sysPermission", permissionService.getSysPermissionById(permission.getId()));
		return "backPage/permission-add";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	//@PreAuthorize("hasAuthority('sys:menu:edit')")
	public Results updatePermission(@RequestBody SysPermission permission)
	{
		return permissionService.updateSysPermission(permission);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	//@PreAuthorize("hasAuthority('sys:menu:del')")
	public Results deletePermission(SysPermission sysPermission)
	{
		return permissionService.delete(sysPermission.getId());
	}


	/**
	 * 获取菜单
	 *
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/menu", method = RequestMethod.GET)
	@ResponseBody
	@OperationLogDetail(detail = "生成系统对应菜单", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysPermission> getMenu(Long userId)
	{
		return permissionService.getMenu(userId);
	}

}
